

var Resource = function Resource( API_URL , provider ){
    this.api_prefix = API_URL;
    this.provider =  { // Vue.http
        get : provider.get.bind(provider) ,
        post : provider.post.bind(provider),
        put : provider.put.bind(provider),
        delete : provider.delete.bind(provider)
    }
};

Resource.prototype = {
    index : function index( param_obj ) {

        var query_str = this.toQuery(param_obj);

        return this.provider.get( this.api_prefix + query_str );
    },
    create : function create( param_obj ) {

        var query_str = this.toQuery(param_obj);

        return this.provider.get( this.api_prefix + '/create' + query_str );
    },
    edit : function edit( id ) {
        return this.provider.get( this.api_prefix + '/' + id + '/edit' );
    },
    update : function update( id , param_obj ) {
        return this.provider.put( this.api_prefix + '/' + id , param_obj );
    },
    destroy : function destroy( id ) {
        return this.provider.delete( this.api_prefix + '/' + id );
    },
    store : function store( params ) {
        return this.provider.post( this.api_prefix , params );
    },
    revise : function store( id , params ) {
        return this.provider.post( this.api_prefix + '/' + id , params );
    },
    show : function show( id , param_obj ) {

        var query_str = this.toQuery(param_obj);

        return this.provider.get( this.api_prefix + '/' + id + query_str );
    },
    get : function get( url , param_obj ) {
        var query_str = this.toQuery(param_obj);
        return this.provider.get( this.api_prefix + '/' + url + query_str );
    },
    post : function post( url , param_obj ) {
        return this.provider.post( this.api_prefix + '/' + url , param_obj );
    },
    toQuery : function( param_obj ) {

        if ( ! param_obj ) return '';

        var filter_keys_arr = Object.keys( param_obj );

        return filter_keys_arr.reduce( function( before, current , index ) {

            // console.log(before + ',' + current + ',' + index);

            var joint_symbol = filter_keys_arr.length - 1 - index  ? '&' : '' ;

            if ( param_obj[current] ) {
                return  before + current + '=' + param_obj[current] + joint_symbol;
            } else {
                return before
            }

        }, '?' );
    }
};